<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019-9-12
 * Time: 10:52
 */
namespace  app\common\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\validate\Admin as adminV;

class Admin extends Model{
    //软删除
    use SoftDelete;

    /**
     * 添加数据
     * @param $data
     * @return array|false|int
     */
    public function addM($data){
        $adminV=new adminV();
        if(!$adminV->scene('add')->check($data)){ //数据验证
            return $adminV->getError();
        }
        $res=$this->allowField(true)->save($data); //添加数据
        if($res){
            return 1; //添加成功
        }else{
            return $res; //添加失败
        }
    }

    /**
     * 修改数据
     * @param $data
     * @return array|false|int
     */
    public function editM($data){
        $adminM=new adminV();
        //验证
        if(!$adminM->scene('edit')->check($data)){
            return $adminM->getError();
        }
        $res=$this->allowField(true)->save($data,['id'=>$data['id']]);
        if($res){
            return 1; //修改成功
        }else{
            return $res; //修改失败
        }
    }

    /**
     * 删除
     * @param $id
     * @return int
     */
    public function delM($id)
    {
        $res=$this->destroy($id);
        if($res){
            return 1; //删除成功
        }else{
            return $res; //删除失败
        }
    }

    /**
     * 查询一条数据
     * @param $id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($id){
      return $this->where('id='.$id)->find()->toArray();
    }


}